package com.bidevalution.business.job;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeEvalutionOutcome;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.google.common.collect.Maps;
import com.qtp.core.util.OConvertUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: 汇总评标分数和排名，约定c16存总分，c17存排名，c18存最终排名
 * @author: xiayf
 * @date: 2019-11-21 17:57
 */
public class EvalutionFinalResultJob implements StepJobJdbcFactory {

    private JdbcTemplateService jdbcTemplateService;

    private Logger log = LogManager.getLogger(getClass());

    @Override
    public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode, String expertId, String layoutCode) throws Exception {
        try {
            Map<String, Object> paramMap = Maps.newHashMap();
            paramMap.put("sectionId", beSectionStepJob.getSectionId());
            paramMap.put("sectionStepId", beSectionStepJob.getSectionStepId());
            paramMap.put("submitCode", submitCode);
            paramMap.put("isDel", "0");
            StringBuilder sb = new StringBuilder();
            sb.append(" SELECT enterprise_id,c16,c17,c18 FROM be_evalution_summary ");
            sb.append(" WHERE section_id=:sectionId AND section_step_id=:sectionStepId AND submit_code=:submitCode AND is_del=:isDel ");
            sb.append(" ORDER BY create_time DESC ");
            //获取此环节投标单位评审结果
            List<BeEvalutionOutcome> dataList = jdbcTemplateService.selectForList(sb.toString(), paramMap, BeanPropertyRowMapper.newInstance(BeEvalutionOutcome.class));
            if (dataList != null && !dataList.isEmpty()) {
                List<BeSectionEnterprise> enterList = new ArrayList<>();
                BeSectionEnterprise beSectionEnterprise;
                for (BeEvalutionOutcome beEvalutionOutcome : dataList) {
                    beSectionEnterprise = new BeSectionEnterprise();
                    beSectionEnterprise.setSeqId(beEvalutionOutcome.getEnterpriseId());
                    beSectionEnterprise.setScore(beEvalutionOutcome.getC16());
                    beSectionEnterprise.setRank(OConvertUtils.getInt(beEvalutionOutcome.getC17()));
                    beSectionEnterprise.setFinalRank(OConvertUtils.getInt(beEvalutionOutcome.getC18()));
                    enterList.add(beSectionEnterprise);
                }
                //批量转数组
                SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(enterList);
                jdbcTemplateService.bathUpdate("UPDATE be_section_enterprise SET score=:score,rank=:rank,final_rank=:finalRank WHERE seq_id=:seqId",
                        beanSources);
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
        this.jdbcTemplateService = jdbcTemplateService;
    }
}
